/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *
 * Funções especificas da nossa aplicacao: Carpooling
 */


/*
 *  Ex - redimensionar, enlarge e shrink
 */


function modalOpen (dialog) {
	dialog.overlay.fadeIn('slow', function () {
		dialog.container.fadeIn('slow', function () {
			dialog.data.hide().slideDown('slow');
		});
	});
}

function modalClose (dialog) {
	dialog.data.fadeOut('slow', function () {
		dialog.container.hide('slow', function () {
			dialog.overlay.slideUp('slow', function () {
				$.modal.close();
			});
		});
	});
}

function showModal(title, toShow)
{
    alert(window.parent.document.body);
    $('#modalContainer').modal({
        onOpen: modalOpen,
        onClose: modalClose,
        onShow: function (dialog) {
            dialog.data.find('.m_title').append(title);
			dialog.data.find('.m_message').append(toShow);
		}
    });
}

function fillReg(data){
    $('#firstname').val($('firstname',data).text());
    $('#lastname').val($('lastname',data).text());
    $('#address').val($('address',data).text());

    gender = $('gender',data).text();
    if (gender == 'M')
        $('#male').attr("checked", true);
    else
        $('#female').attr("checked", true);

    $('#email').val($('email',data).text());
    $('#birth').val($('birth',data).text());
    //$.datepicker.parseDate('yy-mm-dd', '2007-01-26');
    //alert($('birth',data).text());
    //d = $.datepicker.parseDate( 'yy/mm/dd', $('birth',data).text());
    //$('birth').datepicker("setDate", d);
    //$('#birth').datepicker('setDate' , $('birth',data).text());
    
}

function expandRes(b, r){
    if($(b).attr("class")=="colapsed"){
        $(b).css('backgroundImage','url( resources/images/down_btn.gif )');
         $(b).attr("class","expanded");
         $('#'+r).slideDown();
    }
    else{
        $(b).css('backgroundImage','url( resources/images/login_btn.gif )');
        $(b).attr("class","colapsed");
        $('#'+r).hide();
    }
}

function fillDados(){

    $('#nome').val("Sérgio");
    $('#morada').val("Coimbra");
    $('#numero').val("501062974");
    $('#marca12').val("peugeot");
    $('#modelo12').val("206");
    $('#ano12').val("2004");
    $('#matricula12').val("24-42-ZC");
    $('#cor12').val("cinzento");
    $('#nome_balao').val("ssousa");
    
}



function fillConfirmacao(){

    $('#partida').val("Polo I");
    $('#chegada').val("Polo II");
    $('#chegada2').val("Polo II");
    $('#data').val("13-06-2009");
    $('#hora_p').val("14:00");
    $('#hora_c').val("14:15");
    $('#veiculo12').val("peugeot");
    $('#num_p').val("3");
    $('#marca12').val("peugeot");
    $('#modelo12').val("206");
    $('#ano12').val("2004");
    $('#matricula12').val("24-42-ZC");
    $('#cor12').val("cinzento");
    
}


function openRegister(step){
    switch(step) {
        case 1:
            $('#balao_branco').fadeOut('slow');
            //$('#registing').fadeOut('slow');
            $('#main-iframe').attr("src","register.jsp");
            break;
        case 2:
            $.post("FCTUCServlet",
            {
                operation: "finduser",
                id: $('#reg_numero').val()
            },
            function(data){                
                if ($("error",data).length > 0)
                {
                    $('#reg_not_numero').html($("error",data).text());
                    $('#reg_not_numero').slideDown();
                }
                else {
                //success
                $('#reg_not_numero').hide();
                $('#reg_numero').attr("disabled", "disabled");
                fillReg(data);
                $('#reg_step2').slideDown();
            }
            }
            );
            break;
        case 3:
            $('#reg_step3').slideDown();
            break;
        case 4:
            $('#reg_step4').slideDown();
            break;
        default:
            break;
    }

}

    
}

function submitReg(){
    $.post("UserServlet",
            {
                operation: "register",
                number: $('#reg_numero').val(),
                name: $('#firstname').val()+" "+$('#lastname').val(),
                gender: $('input[name=gender]:checked').val(),
                birth: $('#birth').val(),
                //birth: $.datepicker.formatDate('yy-mm-dd', $("#birth").datepicker("getDate")),
                address: $('#address').val(),
                email: $('#email').val(),
                username: $('#rusername').val(),
                password: $('#rpassword').val()
            },
            function(data){
                if ($("error",data).length > 0)
                {
                    //to remove alert
                    alert($("error",data).text());
                    $('#reg_not_numero').html($("error",data).text());
                    $('#reg_error').slideDown();
                }
                else {
                //success
                $('#reg_error').hide();
                showModal("Registo","Registo efectuado com successo. Benvindo ao Carpool... Boas viagens!");
                //abre a pagina principal
                openP();
                
            }
            }
            );

}

function clearField(field)
{
    $(field).val("");
}

function test() {
    alert("test");
}

function clearNotify()
{
    $('#notify-area',top.document).html("");
}

function hideNotify()
{
    $('#notify-area').fadeOut("slow");
}

function notifyError(msg){
    $('#notify-area',top.document).load("error.jsp", {
        'msg': msg
    });
    $('#notify-area',top.document).fadeIn("slow");
}

function notifyWarning(msg){
    $('#notify-area',top.document).load("warning.jsp", {
        'msg': msg
    });
    $('#notify-area',top.document).fadeIn("slow");
}
function notifyInfo(msg){
    $('#notify-area',top.document).load("info.jsp", {
        'msg': msg
    });
    $('#notify-area',top.document).fadeIn("slow");
}


function enlargeTo(obj, size)
{
    if (obj.width <=size) return;
    obj.width = size;
}

function shrinkTo(obj, size)
{
    if (obj.width >= size) return;
    obj.width = size;
}

function lookUser(inputString) {
    if(inputString.length == 0) {
        // Hide the suggestion box.
        $('#suggestions').hide();
    } else {
        $.post("FCTUCServlet",
        {
            queryString: ""+inputString+"",
            operation: "lookup"
        },
        function(data){
            if(data.length >0) {
                $('#suggestions').show();
                $('#autoSuggestionsList').html(data);
            }
        });
    }
} // lookup

function fill(thisValue) {
    $('#inputString').val(thisValue);
    setTimeout("$('#suggestions').hide();", 200);
}

function alertText(text){
    alert(text);
}

function openHome(){
	user = $('#username').val();
    pass = $('#password').val()

    if(user == "" || pass.length == 0)

    $.post("UserServlet",
    {
        operation: "auth",
        user: $('#username').val(),
        pass: $('#password').val()
    },
    function(data){
        //var string = (new XMLSerializer()).serializeToString(data);
        //alert(string);
        }
        );
    
    $('#registing').fadeOut('slow');
    $('#balao_branco').fadeOut('slow');
    $('#main-iframe').attr("src","home.jsp");
    
}


function openFAQ(){
    /*$('#notify-area').fadeOut('slow');*/
    $('#registing').fadeOut('slow');
    $('#balao_branco').fadeOut('slow');
    $('#main-iframe').attr("src","faq.jsp");
    $('#notify-area').fadeOut('slow');
}

function openEnviar(){
    /*$('#notify-area').fadeOut('slow');*/
    $('#registing').fadeOut('slow');
    $('#balao_branco').fadeOut('slow');
    $('#main-iframe').attr("src","enviar.jsp");
    $('#notify-area').fadeOut('slow');
}

function openMapa(){
    $('#notify-area').fadeOut('slow');
    $('#registing').fadeOut('slow');
    $('#balao_branco').fadeOut('slow');
    $('#main-iframe').attr("src","mapa.jsp");
}

function openP(){
    $('#notify-area').fadeIn('slow');
    $('#registing').fadeIn('slow');
    $('#balao_branco').fadeIn('slow');
    $('#main-iframe').attr("src","startup.jsp");
}
function closeHome(){
    $('#balao_branco').fadeIn('slow');
    $('#registing').fadeIn('slow');
    $('#main-iframe').attr("src","startup.jsp");
}

function openMain(){
    $('#right'+' iframe').attr("src","personal_profile_page.jsp");
}

function openSearch(){
    $('#right'+' iframe').attr("src","search.jsp");
}

function openNew(){
    $('#right'+' iframe').attr("src","new_ride.jsp");
}

function pickDate()
{
    $('#date').datepicker();
}

function saveTest(name) {
    $.ajax({
        type: "POST",
        //url: "test/ajaxTest.jsp", <- JSP
        url: "TestServlet", // <- Servlet (foi registada com esta path)
        data: "name="+name,
        success: function(msg){
            alert( msg );
        }
    });
}

function processLogin() {
    alert("ola");

    //        $.ajax({
    //   type: "POST",
    //   //url: "test/ajaxTest.jsp", <- JSP
    //   url: "AuthServlet", // <- Servlet (foi registada com esta path)
    //   data: "username="+$('#username').val()+", password="+$('#password').val(),
    //   success: function(msg){
    //     alert( msg );
    //   }
    // });

    $.post("AuthServlet",{
        user_name:$('#username').val(),
        password:$('#password').val()
    } ,function(data)

    {
            alert(data);
        //          if(data=='yes') //if correct login detail
        //          {
        //                $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
        //                {
        //                  //add message and change the class of the box and start fading
        //                  $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,
        //                  function()
        //                  {
        //                     //redirect to secure page
        //                     $('#main-iframe'+' iframe').attr("src","home.jsp");
        //                     setFocus('iframe');
        //                     //document.location='secure.php';
        //                  });
        //                });
        //          }
        //          else
        //          {
        //                $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
        //                {
        //                  //add message and change the class of the box and start fading
        //                  $(this).html('Your login detail sucks...').addClass('messageboxerror').fadeTo(900,1);
        //                });
        //          }
        });
    return false;//not to post the  form physically
}
